import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split #继承划分训练集和测试机
from sklearn.neighbors import KNeighborsClassifier  #K近邻
import matplotlib.pyplot as plt
from collections import Counter     #用于统计分类正确的样本数
from sklearn.preprocessing import StandardScaler#导入用于归一化的包
from sklearn.model_selection import cross_val_score#k折
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import BaggingClassifier

AF_input_frame=pd.read_excel('pre_treatment_data_feature_1.xlsx',sheet_name='AF_input')
nonAF_input_frame=pd.read_excel('pre_treatment_data_feature_1.xlsx',sheet_name='nonAF_input')
AF_label_frame=pd.read_excel('pre_treatment_data_feature_1.xlsx',sheet_name='AF_label')
nonAF_label_frame=pd.read_excel('pre_treatment_data_feature_1.xlsx',sheet_name='nonAF_label')

AF_input=AF_input_frame.to_numpy()
nonAF_input=nonAF_input_frame.to_numpy()
AF_label=AF_label_frame.to_numpy()
nonAF_label=nonAF_label_frame.to_numpy()


AF_label=AF_label.flatten()
nonAF_label=nonAF_label.flatten()


X=np.concatenate([AF_input,nonAF_input])
from sklearn.decomposition import PCA
#pca=PCA(n_components='mle',svd_solver='full')
raw_y=np.concatenate([AF_label[0:],nonAF_label[0:]])
acc_list=[]
for i in range(1,7):
    X1=X[:,[2,3,4,5,6,7]]
    pca=PCA(n_components=i,svd_solver='full')
    pca.fit(X1)
    raw_x=pca.transform(X1)


    #raw_x=X_PCA
    #raw_X=X

    ss=StandardScaler()
    raw_x=ss.fit_transform(raw_x)

    k=10
    m=400
    m_min=460
    m_max=461
    m_interval=2
    index=np.array(range(m_min,m_max+1,m_interval))
    for i in range(m_min,m_max+1,m_interval):
        model=KNeighborsClassifier(n_neighbors=i,weights='uniform')
        ada_clf = BaggingClassifier(estimator = model,n_estimators=100)
        acc=cross_val_score(model,raw_x,raw_y,cv=k,scoring="accuracy")
        mean_acc=np.array(acc).mean()
        print(mean_acc)
        acc_list.append(mean_acc)
plt.plot(np.array(range(1,7)),np.array(acc_list))
plt.xlim(1,7)
plt.show()